<template>
  <div class="goods-list">
    <!-- 1.0 左侧菜单 -->
    <div class="list-left" ref="listLeft">
      <ul>
        <li
          :class="curStr === item.name ? 'active' : ''"
          v-for="item in goodsList"
          :key="item.name"
          @click="changeCur(item.name)"
        >
          <span>{{ item.name }}</span>
        </li>
      </ul>
    </div>

    <!-- 2.0 右侧详情列表 -->
    <div class="list-right" ref="listRight">
      <ul>
        <li v-for="item in goodsList" :key="item.name" :id="item.name">
          <h3 class="cate-title">{{ item.name }}</h3>
          <ul>
            <li v-for="sub in item.foods" :key="sub.id" class="list-item">
              <img :src="sub.imgUrl" alt="" @click="showDetail(sub)" />
              <div>
                <h4 @click="showDetail(sub)">{{ sub.name }}</h4>
                <p class="desc">{{ sub.goodsDesc }}</p>
                <p>
                  <span>月售{{ sub.sellCount }}份</span>
                  <span>好评率{{ sub.rating }}%</span>
                </p>
                <div class="price">
                  <span><small>￥</small>{{ sub.price }}</span>
                  <span
                    ><del>￥{{ (sub.price + 10).toFixed(2) }}</del></span
                  >
                </div>
                <van-stepper
                  :class="sub.num ? 'showNum' : ''"
                  v-model="sub.num"
                  theme="round"
                  button-size="22"
                  min="0"
                  disable-input
                  :show-minus="Boolean(sub.num)"
                  @plus="addNum"
                />
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>

    <!-- 3.0 显示遮罩层 -->
    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="goods-detail">
          <Detail :sonData="curDetail" @closeMask="show = false" />
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import Detail from './componets/detail.vue'
import { mapState, mapMutations } from 'vuex'
// 引入插件
import BetterScroll from 'better-scroll'

export default {
  components: {
    Detail
  },
  data() {
    return {
      curStr: '',
      rightObj: {},
      rangeArr: [],
      show: false,
      curDetail: {}
    }
  },
  computed: {
    ...mapState(['goodsList'])
  },
  mounted() {
    // 完成左侧的滑动、
    setTimeout(() => {
      this.setRange()

      // 设置滚动的实例
      this.setObj()
    }, 500)
  },
  methods: {
    ...mapMutations(['changeFlag']),
    changeCur(s) {
      // 当点击左侧菜单的时候，不仅要高亮，还要让右侧的盒子滑动
      this.curStr = s
      const box = document.getElementById(s)
      this.rightObj.scrollToElement(box, 400)
    },
    setRange() {
      // 声明了滑动的范围值
      let total = 0
      this.goodsList.forEach(item => {
        const tarHeight = document.getElementById(item.name).offsetHeight
        // 记录每一条数据的最大最小的范围和name值
        this.rangeArr.push({
          min: total,
          max: total + tarHeight,
          name: item.name
        })
        total = total + tarHeight
      })
    },
    // 设置滚动的实例
    setObj() {
      const leftObj = new BetterScroll(this.$refs.listLeft, {
        click: true
      })

      // 右侧的实例
      this.rightObj = new BetterScroll(this.$refs.listRight, {
        click: true,
        probeType: 3
      })
      // 注册滑动事件
      this.rightObj.on('scroll', ({ y }) => {
        // 取绝对值
        y = Math.abs(y)
        // 对范围进行遍历，满足条件就让当前高亮的类名更新
        for (const v of this.rangeArr) {
          if (y >= v.min && y <= v.max) {
            this.curStr = v.name
          }
        }
      })
    },
    // 展示详情
    showDetail(item) {
      this.curDetail = item
      this.show = true
    },
    addNum() {
      // 需要让购物车的那个div加上一个big的类名，其实就是操作那个flag
      this.changeFlag()
    }
  },
  watch: {
    goodsList() {
      this.curStr = this.goodsList[0].name
    }
  }
}
</script>

<style lang="less" scoped>
.goods-list {
  position: relative;
  z-index: 1000;
  height: 100%;
  display: flex;
  overflow: hidden;

  .list-left {
    height: 100%;
    flex: 0 0 80px;
    font-size: 14px;

    li {
      padding: 0 10px;
      span {
        display: block;
        border-bottom: 1px solid #e4e4e4;
        padding: 15px 0;
        color: #666;
      }

      &.active {
        background-color: #fff;
        span {
          color: #f80;
        }
      }
    }
  }
  .list-right {
    height: 100%;
    flex: 1;
    background-color: #fff;
    overflow: hidden;

    > ul {
      padding-bottom: 200px;
    }

    .cate-title {
      line-height: 30px;
      text-indent: 1em;
      background-color: #f8f8f8;
      color: #999;
      font-weight: normal;
      border-left: 2px solid #ccc;
      box-sizing: border-box;
      font-size: 14px;
    }

    img {
      width: 70px;
      height: 70px;
      margin-right: 10px;
      border: 1px solid #f8f8f8;
      box-shadow: 0px 5px 6px rgba(0, 0, 0, 0.08);
    }

    .list-item {
      width: 100%;
      display: flex;
      font-size: 12px;
      padding: 10px;
      color: #333;
      box-sizing: border-box;

      > div {
        position: relative;
        width: 70%;
        padding-right: 15px;
        box-sizing: border-box;
      }

      .desc {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      h4 {
        font-size: 14px;
        font-weight: normal;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      p {
        color: #999;
        margin-bottom: 3px;
      }
      .price {
        span {
          &:nth-of-type(1) {
            color: #f12321;
            font-size: 14px;
            margin-right: 5px;
            font-weight: 700;
          }
        }
      }

      // 步进器
      .van-stepper {
        position: absolute;
        right: 10px;
        bottom: -6px;

        &.showNum {
          /deep/ input {
            // 显示
            visibility: visible;
          }
        }

        /deep/.van-stepper__plus {
          background-color: #00b5fd;
        }
        /deep/input {
          // 隐藏
          visibility: hidden;
        }

        /deep/.van-stepper__minus {
          color: #d300a5c7;
          border-color: #0080b3c7;
        }
      }
    }
  }
}

.goods-detail {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #fff;
}
</style>
